<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="/plugins/select2/select2.css" />
	<link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
	<script src="/plugins/select2/select2.min.js" type="text/javascript"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="listaaa">

	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteCats()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="listCat()" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th>
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="catShow">

			                      </tbody>
			                  </table>
							<!--数据列表/-->
						
                        </div>
                        <!-- 数据表格 /-->

                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td>
		      		   珠宝 >>  银饰
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input id="catName" class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td id="templateTd">

		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" onclick="addCat()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 编辑窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3>商品分类编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <td>
							  <input type="hidden" id="catId">
						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <td><input id="catName2" class="form-control" placeholder="商品分类名称">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td id="templateTd2">

						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" onclick="updateCat()" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
<script>
	$(function (){
		listCat()
		listTemplate()
		listTemplate2()
		$("#templateSelect2").select2()
	})
	function listCat(id){
		$.ajax({
			url: "/cat/listCat",
			data: {pid:id},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				console.log(result)
				if(result.code == "666"){
					var cats = result.data
					var html = ""
					if (cats.length == 0){
						html +="<tr><td><a type='javascript:void(0)' onclick='listCat()'>没有下一级了,点击返回</a></td></td></tr>"
						$("#catShow").html(html)
					}else{
						for (var i = 0; i < cats.length; i++) {
							html += '<tr>'
							html += '<td><input type="checkbox" value="'+ cats[i].id +'"></td>'
							html += '<td>'+ cats[i].id +'</td>'
							html += '<td>'+ cats[i].name +'</td>'
							html += '<td>'
							html += cats[i].typeId
							html += '</td>'
							html += '<td className="text-center">'
							html += '<button type="button" className="btn bg-olive btn-xs" onclick="listCat('+ cats[i].id +')">查询下级</button>'
							html +=	' <button type="button" className="btn bg-olive btn-xs" data-toggle="modal" onclick="toUpdate('+ cats[i].id +')" data-target="#updateModal">修改</button>'
							html += '</td>'
							html += '</tr>'
						}
						$("#catShow").html(html);
					}
				}else{
					$("#catShow").html("<tr><td>" + result.message + "</td></td></tr>");
				}

			},
			error: function (){
				alert("系统异常")
			}
		})
		navaspector()
	}
	function listTemplate(){
		$.ajax({
			url: "/cat/listTemplate",
			//data: {pageNum:num,name:$("#selectByName").val()},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				var html = ""
				if(result.code == 666){
					var listTemplate = result.data;
					html +='<select id="templateSelect" style="width:100%">'
					$(listTemplate).each(function (i,e){
						html += '<option value="' + e.id +'">'
						html +=	e.name
						html += '</option>'
					})
					html += '</select>'
					$("#templateTd").html(html);
				}else{
					$("#templateTd").html("<tr><td>" + result.message + "</td></td></tr>");
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function listTemplate2(){
		$.ajax({
			url: "/cat/listTemplate",
			//data: {pageNum:num,name:$("#selectByName").val()},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				var html = ""
				if(result.code == 666){
					var listTemplate = result.data;
					html +='<select id="templateSelect2" style="width:100%">'
					$(listTemplate).each(function (i,e){
						html += '<option value="' + e.id +'">'
						html +=	e.name
						html += '</option>'
					})
					html += '</select>'
					$("#templateTd2").html(html);
				}else{
					$("#templateTd2").html("<tr><td>" + result.message + "</td></td></tr>");
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function addCat(){
		var i = $("#templateSelect").val()
		var name = $("#catName").val()
		$.ajax({
			url: "/cat/addCat",
			data: {typeId:i,name:name},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == 666){
					alert(result.message)
					listCat(result.data)
				}else{
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function toUpdate(id){
		$.ajax({
			url: "/cat/toUpdate",
			data: {id:id},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == 666){
					var cat = result.data
					$("#catId").val(cat.id)
					$("#catName2").val(cat.name)
					var id = cat.typeId
					$("#templateSelect2").val(id).trigger('change')
				}else{
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function updateCat(){
		var id= $("#catId").val()
		var name = $("#catName2").val()
		var temp = $("#templateSelect2").select2('data')
		var t = temp.id
		$.ajax({
			url: "/cat/updateCat",
			data: {id:id,name:name,typeId:t},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == 666){
					alert(result.message)
					listCat(result.data)
				}else{
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function deleteCats(){
		if(confirm("是否删除")){
			var ids = []  //定义一个数组
			$("[type=checkbox]").each(function (i, e) {   //获取所有复选框的value,循环
				if ($(e).prop("checked")) {   //判断是否被选中
					ids.push($(e).val())  //选中就添加到数组中
				}
			})
			if (ids.length <= 0){
				alert("请选择要删除的数据")
				return
			}
			$.ajax({
				url: "/cat/deleteCats?ids=" + ids,
				type: "get",
				dataType: "json",
				async: false,
				success: function (result) {
					if (result.code == "666") {
						if(result.data == 0){
							alert(result.message)
							listCat();
						}else {
							alert("该分类有子集,请先删除子集")
						}
					}else{
						alert(result.message)
					}
				},
				error: function (){
					alert("系统异常")
				}
			})
		}
	}
	function navaspector(){
		$.ajax({
			url: "/cat/navaspector",
			type: "get",
			dataType: "json",
			async: false,
			success: function (result) {
				if (result.code == "666") {
					var html = ""
					html +='<li>'
					html +='<a type="JavaScript:void(0)" onclick="listCat(0)" >顶级分类列表</a>'
					html +='</li>'
					var list = result.data;
					console.log(list)
					if(list != null){
						$(list).each(function (i,e){
							html += '<li>'
							html += '<a type="JavaScript:void(0)">'+ e.name +'</a>'
							html += '</li>'
						})
					}
					$("#listaaa").html(html)
				} else {
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
</script>
</html>